package com.finance.base.exception;

import com.finance.base.constant.BaseConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class BaseException extends RuntimeException {

    private static final long serialVersionUID = -6202759933628287239L;

    private static Logger logger = LoggerFactory.getLogger(BaseException.class);

    private String errorCode;

    private String errorMsg;

    private List<String> params = new ArrayList<String>(0);


    /**
     * 默认构造，一般用于获取动态方法堆栈
     */
    public BaseException() {
        super();
        logger.warn("操作执行异常");
    }

    /**
     * 根据编码构造异常
     *
     * @param code 返回码
     */
    public BaseException(String code) {
        String msg = BaseConstants.getErrorMessage(code);
        this.errorCode = code;
        this.errorMsg = msg;
    }

    /**
     * 根据异常信息和返回码构造异常
     *
     * @param code
     * @param message
     */
    public BaseException(String code, String message) {
        this.errorCode = code;
        this.errorMsg = message;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public void addParam(String p) {
        params.add(p);
    }

    public BaseException(String errorCode, List<String> params) {
        this.errorCode = errorCode;
        this.params = params;
        if (errorMsg == null) {
            for (String s : params) {
                if(errorMsg == null){
                    this.errorMsg = s;
                }else{
                    this.errorMsg += s;
                }
            }
        }
    }

    public BaseException(String errorCode, String[] params) {
        this.errorCode = errorCode;
        for (String s : params) {
            this.params.add(s);
        }
        if (errorMsg == null) {
            for (String s : params) {
                if(errorMsg == null){
                    this.errorMsg = s;
                }else{
                    this.errorMsg += s;
                }
            }
        }
    }

    public List<String> getParams() {
        return params;
    }

    public void setParams(List<String> params) {
        this.params = params;
    }
}
